package com.xca.znb.view;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.xca.znb.R;
import com.xca.znb.contact.SaleMainContact;
import com.xca.znb.presenter.SaleMainPresenter;

public class SaleMain extends AppCompatActivity implements View.OnClickListener  , SaleMainContact {
    private LinearLayout SaleHome , SaleSchool , SalePublish , SaleMessage , SaleMines ;
    private SaleMainPresenter saleMainPresenter ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_sales);
        SaleHome = findViewById(R.id.sale_user_home);
        SaleSchool = findViewById(R.id.sale_school);
        SalePublish = findViewById(R.id.sale_publish);
        SaleMessage = findViewById(R.id.sale_my_messages);
        SaleMines = findViewById(R.id.sale_mines);
        saleMainPresenter = new SaleMainPresenter(this);
        //设置默认
        SaleHome.setSelected(true);
        replaceFragment(new SaleHomeFragment());

        onSaleHomeButtonClick();
        onSaleSchoolButtonClick();
        onSalePublishButtonClick();
        onSaleMessageButtonClick();
        onSaleMinesButtonClick();
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.sale_user_home){
            SaleHome.setSelected(true);
            SaleSchool.setSelected(false);
            SalePublish.setSelected(false);
            SaleMessage.setSelected(false);
            SaleMines.setSelected(false);
            replaceFragment(new SaleHomeFragment());
        } else if (v.getId()==R.id.sale_school) {
            SaleHome.setSelected(false);
            SaleSchool.setSelected(true);
            SalePublish.setSelected(false);
            SaleMessage.setSelected(false);
            SaleMines.setSelected(false);
            replaceFragment(new FriendsFragment());
        } else if (v.getId()==R.id.sale_publish) {
            SaleHome.setSelected(false);
            SaleSchool.setSelected(false);
            SalePublish.setSelected(true);
            SaleMessage.setSelected(false);
            SaleMines.setSelected(false);
            replaceFragment(new PublishFragment());
        } else if (v.getId()==R.id.sale_my_messages) {
            SaleHome.setSelected(false);
            SaleSchool.setSelected(false);
            SalePublish.setSelected(false);
            SaleMessage.setSelected(true);
            SaleMines.setSelected(false);
            replaceFragment(new MessageFragment());
        }else if (v.getId()==R.id.sale_mines){
            SaleHome.setSelected(false);
            SaleSchool.setSelected(false);
            SalePublish.setSelected(false);
            SaleMessage.setSelected(false);
            SaleMines.setSelected(true);
            replaceFragment(new MineFragment());
        }
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();//创建fragment管理器
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.Sale_Home_Fragment , fragment);//替换对象
        // transaction.addToBackStack(null);//将fragment添加到栈中，达到点击返回移除栈的效果,会造成栈的大量堆叠
        transaction.commit();
    }

    @Override
    public void onSaleHomeButtonClick() {
        SaleHome.setOnClickListener(this);
    }

    @Override
    public void onSaleSchoolButtonClick() {
        SaleSchool.setOnClickListener(this);
    }

    @Override
    public void onSalePublishButtonClick() {
        SalePublish.setOnClickListener(this);
    }

    @Override
    public void onSaleMessageButtonClick() {
        SaleMessage.setOnClickListener(this);
    }

    @Override
    public void onSaleMinesButtonClick() {
        SaleMines.setOnClickListener(this);
    }
}
